var ValidDateSetting = function () {
  var template = /* html */ `
    <div class="valid-date-setting" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto" size="medium">
        <el-form-item label="公司名称">
          <span>{{companyForm.name}}</span>
        </el-form-item>
        <el-form-item label="支付时间">
          <el-date-picker v-model="companyForm.payTime" class="setting-input" value-format="timestamp" :editable="false"
            :clearable="false">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间">
          <el-date-picker v-model="companyForm.endTime" class="setting-input" value-format="timestamp" :editable="false"
            :clearable="false">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提醒时间">
          <el-date-picker v-model="companyForm.clockTime" class="setting-input" value-format="timestamp" :editable="false"
            :clearable="false">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div style="text-align:center">
        <el-button size="medium" type="primary" @click="ensure">确定</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      company: {
        type: Object,
        required: true
      }
    },

    data: function () {
      var company = this.company;
      var companyForm = {
        name: company.name,
        payTime: company.payTime,
        endTime: company.endTime,
        clockTime: company.clockTime
      };
      return {
        loading:false,
        companyForm: companyForm
      };
    },

    methods: {
      ensure: function () {
        var code = this.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg('companyAuthoritySetting.error.' + code);
          this.$alert(msg, '信息');
          return;
        }

        var companyForm = this.companyForm;
        var postData = {
          companyName:companyForm.name
        };
        
        var fields = ['payTime', 'endTime', 'clockTime'];
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var timeValue = companyForm[field];
          postData[field] = moment(timeValue).format('YYYY-MM-DD');
        }

        this.loading = true;
        var self = this;
        return ajaxRequest('company/updateComapnyPayEndClockTime',postData).then(function(){
          self.loading = false;
          self.$alert('该公司有效时间设置成功','信息',{
            callback:function(){
              self.$emit('setting-success');
            }
          });
        }).catch(function(){
          self.loading = false;
        });
      },

      checkValid: function () {
        var companyForm = this.companyForm;
        var fields = ['payTime', 'endTime', 'clockTime'];
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = companyForm[field];
          if (value === null) {
            return field;
          }
        }

        var endTime = companyForm.endTime;
        var payTime = companyForm.payTime;
        if (moment(payTime).isAfter(endTime,'day')) {
          return 'payTime>endTime';
        }

        var clockTime = companyForm.clockTime;
        if (moment(clockTime).isAfter(endTime,'day')) {
          return 'clockTime>endTime';
        }

        return '';
      }
    },

    template: template
  };
}();
